#include "password_input.h"
#include "ui_password_input.h"
#include <math.h>
#include <main.h>



Password_input::Password_input(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::Password_input)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    password_mode=0;
    whichpassword=0;
    enter=0;
    ui->lineEdit->setReadOnly (true );
}

Password_input::~Password_input()
{
    delete ui;

}


bool Password_input::password_check(QString password,bool block)
{
    if(whole_machine_parameter.password_protect)
    {
    setpassword(password);
    if(block)   ui->esc->setDisabled(true);
     exec();
    if(block)   ui->esc->setEnabled(true);
    return password_right;
    }

}



void Password_input::Display(int num)
{
   if(password_error)
   { if(password_mode)ui->lineEdit->setEchoMode (QLineEdit::Password);
     else  ui->lineEdit->setEchoMode (QLineEdit::Normal);
     ui->lineEdit->clear();
     password_error=0;
   }
   enter=0;
   display=ui->lineEdit->text()+QString::number(num);
   ui->lineEdit->setText(display);

}

void Password_input::on_negative_clicked()
{
    QString str=ui->lineEdit->text();
      BUTTON_SOUND;
   if(str.startsWith('-'))
  {
     display=str.right(str.length() -1);
    }
   else{
         display="-"+ str;
        }
    ui->lineEdit->setText(display);
}

void Password_input::on_dot_clicked()
{  // BUTTON_SOUND;

}

void Password_input::on_clear_clicked()
{  BUTTON_SOUND;
  ui->lineEdit->backspace();

}

void Password_input::on_esc_clicked()
{
    BUTTON_SOUND;
    ui->lineEdit->clear();
    textword="";
    password_right=0;
    enter=0;
    if(password_mode) ui->lineEdit->setEchoMode (QLineEdit::Password);
}

void Password_input::on_enter_clicked()
{  BUTTON_SOUND;
    textword=ui->lineEdit->text();
    ui->lineEdit->clear();
    if(password_mode)
   {
    if(goup) {
        if(textword==passwordgoup[0]) {whichpassword=1;password_right=1;close();}
        else if(textword==passwordgoup[1]){whichpassword=2;password_right=1;close();}
        else if(textword==passwordgoup[2]) {whichpassword=3;password_right=1;close();}
        else {
             whichpassword=0;
             password_right=0;
             password_error=1;
             ui->lineEdit->setEchoMode (QLineEdit::Normal);
             ui->lineEdit->setText(trUtf8("密码输入错误！"));
             }
             }
    else {
    if(textword==password) {password_right=1;close();}
    else {
         password_right=0;
         password_error=1;
         ui->lineEdit->setEchoMode (QLineEdit::Normal);
         ui->lineEdit->setText(trUtf8("密码输入错误！"));
         }
       }
    }
    else {enter=1; //ui->lineEdit->setEchoMode (QLineEdit::Normal);
    }
}
void Password_input::cancel_password_mode( )
{
    password_mode=0;
    ui->lineEdit->setEchoMode (QLineEdit::Normal);
}
void Password_input::setpassword( QString  password)
 {
   goup=0;
   password_mode=1;
   password_error=1;
   this->password= password;

 }

  void Password_input::setpasswordgoup( QString  password1,
                                        QString  password2,QString  password3)
 {
      goup=1;
      password_mode=1;
      password_error=1;
      passwordgoup[0]=password1;
      passwordgoup[1]=password2;
      passwordgoup[2]=password3;
  }

 int Password_input::return_value()
 {
     int value;
     uint lg= textword.length();
      if(textword.startsWith('-'))
      {
      value=0 - textword.right(lg-1).toInt();
      }
      else value=textword.toInt();
      return value;
 }

 void Password_input::on_num_0_clicked()
 {  BUTTON_SOUND;

   Display(0);
 }

 void Password_input::on_num_1_clicked()
 {  BUTTON_SOUND;
     Display(1);
 }
 void Password_input::on_num_2_clicked()
 {  BUTTON_SOUND;
      Display(2);
 }
 void Password_input::on_num_3_clicked()
 {  BUTTON_SOUND;
      Display(3);
 }
 void Password_input::on_num_4_clicked()
 {  BUTTON_SOUND;
      Display(4);
 }
 void Password_input::on_num_5_clicked()
 {  BUTTON_SOUND;
      Display(5);
 }
 void Password_input::on_num_6_clicked()
 {  BUTTON_SOUND;
      Display(6);
 }
 void Password_input::on_num_7_clicked()
 {  BUTTON_SOUND;
      Display(7);
 }
 void Password_input::on_num_8_clicked()
 {  BUTTON_SOUND;
      Display(8);
 }
 void Password_input::on_num_9_clicked()
 {  BUTTON_SOUND;
      Display(9);
 }
